using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerContrall : MonoBehaviour
{
   public float moveSpeed=5f;

   private MapGenerat mapGenerat;
   private Vector2Int currentGridPos;
    // Start is called before the first frame update
    void Start()
    {
        mapGenerat=FindObjectOfType<MapGenerat>();
        currentGridPos=mapGenerat.GetPlayerGridPosition();
    }

    // Update is called once per frame
    void Update()
    {
        Move();
    }

    void Move(){
        Vector2Int moveDir=Vector2Int.zero;

        if(Input.GetKeyDown(KeyCode.W)) moveDir.y+=1;
        if(Input.GetKeyDown(KeyCode.S)) moveDir.y-=1;
        if(Input.GetKeyDown(KeyCode.A)) moveDir.x-=1;
        if(Input.GetKeyDown(KeyCode.D)) moveDir.x+=1;
        if(moveDir!=Vector2Int.zero){
            Vector2Int targetpos=currentGridPos+moveDir;
            if(mapGenerat.IsMoveValid(targetpos)){
                currentGridPos=targetpos;
                mapGenerat.MovePlayer(targetpos);
            }
        }
    }
}
